<template>
  <el-container class="layout-container">
    <el-aside width="200px" class="aside">
      <div class="logo">
        <img src="@/assets/logo.png" alt="logo">
        <span>膳食推荐系统</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="menu"
        :router="true"
        :collapse="isCollapse"
      >
        <el-menu-item index="/">
          <el-icon><House /></el-icon>
          <template #title>首页</template>
        </el-menu-item>
        <el-menu-item index="/profile">
          <el-icon><User /></el-icon>
          <template #title>个人信息</template>
        </el-menu-item>
        <el-menu-item index="/meal-record">
          <el-icon><Calendar /></el-icon>
          <template #title>膳食记录</template>
        </el-menu-item>
        <el-menu-item index="/meal-plan">
          <el-icon><Calendar /></el-icon>
          <template #title>膳食计划</template>
        </el-menu-item>
        <el-menu-item index="/nutrition">
          <el-icon><DataLine /></el-icon>
          <template #title>营养分析</template>
        </el-menu-item>
        <el-menu-item index="/recipes">
          <el-icon><Food /></el-icon>
          <template #title>食谱推荐</template>
        </el-menu-item>
        <el-menu-item index="/goals">
          <el-icon><Aim /></el-icon>
          <template #title>目标设定</template>
        </el-menu-item>
        <el-menu-item index="/settings">
          <el-icon><Setting /></el-icon>
          <template #title>系统设置</template>
        </el-menu-item>
        <el-menu-item index="/food">
          <el-icon><Apple /></el-icon>
          <template #title>食物搜索</template>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-icon class="collapse-btn" @click="toggleCollapse">
            <Fold v-if="!isCollapse"/>
            <Expand v-else/>
          </el-icon>
          <el-breadcrumb>
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ route.meta.title }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-switch
            v-model="isDark"
            class="theme-switch"
            inline-prompt
            :active-icon="Moon"
            :inactive-icon="Sunny"
            @change="toggleTheme"
          />
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :size="32" :src="userInfo?.avatar"/>
              <span>{{ userInfo?.nickname }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useStore } from 'vuex'
import { useRoute, useRouter } from 'vue-router'
import { 
  House, 
  User, 
  Calendar, 
  DataLine, 
  Food, 
  Aim, 
  Setting, 
  Moon, 
  Sunny, 
  Fold, 
  Expand,
  Apple 
} from '@element-plus/icons-vue'

const store = useStore()
const route = useRoute()
const router = useRouter()
const isCollapse = ref(false)
const isDark = computed(() => store.state.user.theme === 'dark')
const userInfo = computed(() => store.state.user.userInfo)
const activeMenu = computed(() => route.path)

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const toggleTheme = () => {
  store.dispatch('user/toggleTheme')
}

const handleCommand = (command) => {
  if (command === 'logout') {
    store.commit('user/CLEAR_USER')
    router.push('/login')
  } else if (command === 'profile') {
    router.push('/profile')
  }
}
</script>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  
  .aside {
    background-color: var(--el-menu-bg-color);
    transition: width 0.3s;
    
    .logo {
      height: 60px;
      display: flex;
      align-items: center;
      padding: 0 20px;
      
      img {
        height: 32px;
        margin-right: 12px;
      }
      
      span {
        font-size: 18px;
        font-weight: bold;
        color: var(--el-text-color-primary);
      }
    }
    
    .menu {
      border-right: none;
    }
  }
  
  .header {
    background-color: var(--el-bg-color);
    border-bottom: 1px solid var(--el-border-color-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    
    .header-left {
      display: flex;
      align-items: center;
      
      .collapse-btn {
        margin-right: 20px;
        cursor: pointer;
        font-size: 20px;
      }
    }
    
    .header-right {
      display: flex;
      align-items: center;
      gap: 20px;
      
      .theme-switch {
        margin-right: 20px;
      }
      
      .user-info {
        display: flex;
        align-items: center;
        cursor: pointer;
        
        span {
          margin-left: 8px;
        }
      }
    }
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 